package org.buptsse.thread;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.message.BasicNameValuePair;
import org.buptsse.utils.APPConstant;
import org.buptsse.utils.ConnectionChecker;
import org.buptsse.utils.HttpHelper;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

public class UpdateSellThread extends Thread {

	private Context context;
	private Handler handler;
	private String sellid,condition,price,booknumber,comment,longtitude,latitude,publisher,author;
	public void setLongtitude(String longtitude) {
		this.longtitude = longtitude;
	}

	public void setLatitude(String latitude) {
		this.latitude = latitude;
	}

	private boolean isLocated=false;
	
	public UpdateSellThread(Context context,Handler handler,String sellid,String condition,String price,String booknumber,String comment,String publisher,String author)
	{
		this.context=context;
		this.handler=handler;
		this.sellid=sellid;
		this.condition=condition;
		this.price=price;
		this.booknumber=booknumber;
		this.comment=comment;
		this.publisher=publisher;
		this.author=author;
		isLocated=false;
	}
	
	public UpdateSellThread(Context context,Handler handler,String sellid,String condition,String price,String booknumber,String comment,String publisher,String author,String longtitude,String latitude)
	{
		this.context=context;
		this.handler=handler;
		this.sellid=sellid;
		this.condition=condition;
		this.price=price;
		this.booknumber=booknumber;
		this.comment=comment;
		this.publisher=publisher;
		this.author=author;
		this.longtitude=longtitude;
		this.latitude=latitude;
		isLocated=true;
	}
	
	public void run()
	{
		if(ConnectionChecker.CheckConnection(context, handler)==false)
		{
			return;
		}
		ArrayList<NameValuePair> param=new ArrayList<NameValuePair>();
		param.add(new BasicNameValuePair("sellid", sellid));
		param.add(new BasicNameValuePair("condition", condition));
		param.add(new BasicNameValuePair("price", price));
		param.add(new BasicNameValuePair("booknumber", booknumber));
		param.add(new BasicNameValuePair("comment", comment));
		param.add(new BasicNameValuePair("publisher", publisher));
		param.add(new BasicNameValuePair("author", author));
		if(isLocated==true)
		{
			param.add(new BasicNameValuePair("longtitude", longtitude));
			param.add(new BasicNameValuePair("latitude", latitude));
		}
		try {
			String result=HttpHelper.sendPost(APPConstant.updateSellUrl, param);
			System.out.println("update sell result:"+result);
			if(result.contains("success"))
			{
				Message msg=handler.obtainMessage(APPConstant.updateSellFinish);
				handler.sendMessage(msg);
			}else {
				Message msg=handler.obtainMessage(APPConstant.updateSellError);
				handler.sendMessage(msg);
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
